package com.leetcode.algorithm.y22.m09.w4;

/**
 * 2073. 买票需要的时间
 * 
 * https://leetcode.cn/problems/time-needed-to-buy-tickets/
 * 
 * @author jie.deng
 *
 */
class Question2073Solution01 {

	public int timeRequiredToBuy(int[] tickets, int k) {
		int sum = 0;
		int len = tickets.length;
		int cnt = tickets[k];
		for (int i = 0; i <= k; i++) {
			sum += (tickets[i] >= cnt ? cnt : tickets[i]);
		}
		cnt = cnt - 1;
		if (cnt > 0) {
			for (int i = k + 1; i < len; i++) {
				sum += (tickets[i] >= cnt ? cnt : tickets[i]);
			}
		}
		return sum;
	}

}